package cn.aihongbo.kmp.base.widget

import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.interaction.MutableInteractionSource
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.navigationBarsPadding
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import org.jetbrains.compose.resources.painterResource
import org.jetbrains.compose.resources.stringResource

@Composable
fun HTabBar(
    destinations: List<HNavigation>,
    currentDestination: String,
    onNavigationTo: (Int) -> Unit,
    modifier: Modifier = Modifier
) {
    Row(
        modifier = Modifier.fillMaxWidth().navigationBarsPadding().background(Color.White)
            .padding(top = 8.dp)
    ) {
        destinations.forEachIndexed { index, navigation ->
            val selected = navigation.route == currentDestination
            val color = if (selected) Color.Black else Color(0xFF999999)
            Column(
                horizontalAlignment = Alignment.CenterHorizontally,
                modifier = Modifier.weight(1f).clickable(
                    interactionSource = remember { MutableInteractionSource() },
                    indication = null,
                    onClick = {
                        onNavigationTo(index)
                    })
            ) {
                Image(
                    painter = painterResource(if (selected) navigation.selectedIcon else navigation.unSelectedIcon),
                    contentDescription = navigation.name,
                    modifier = Modifier.size(22.dp)
                )
                Spacer(modifier = Modifier.width(5.dp))
                Text(text = stringResource(navigation.title), color = color, fontSize = 10.sp)
            }
        }
    }
}